package com.hb.software.holiday.impl;

import com.hb.software.entity.holiday.HolidayMessage;
import com.hb.software.holiday.HolidayService;
import com.hb.software.holiday.dao.HolidayDao;
import com.hb.software.service.impl.BaseServiceImpl;
import com.hb.software.worker.dao.WorkerDao;
import com.hb.software.worker.impl.WorkerServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

@Service
@Transactional
public  class HolidayServiceImpl extends BaseServiceImpl<HolidayMessage,Long> implements HolidayService {

    private static final Logger logger = LogManager.getLogger(HolidayServiceImpl.class.getName());
    private HolidayDao holidayDao;

    @Autowired
    public void setHolidayDao(HolidayDao holidayDao) {
        this.holidayDao = holidayDao;
        this.dao = this.holidayDao;
    }

    @Override
    public long findLastId() {
        return holidayDao.findLastId();
    }

    @Override
    public List findByName(String name) {
        return holidayDao.findByName(name);
    }

    @Override
    public List findByStates(String states) {
        return  holidayDao.findByStates(states);
    }

    @Override
    public String createExcel(List<HolidayMessage> list, String path) {
        // 创建一个Excel文件
        HSSFWorkbook holidaybook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = holidaybook.createSheet("假期表");
        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = holidaybook.createCellStyle();
//    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("编号");
        headCell.setCellStyle(cellStyle);
        headCell = hssfRow.createCell(1);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);
        headCell = hssfRow.createCell(2);
        headCell.setCellValue("开始日期");
        headCell.setCellStyle (cellStyle);
        headCell = hssfRow.createCell(3);
        headCell.setCellValue("结束日期");
        headCell.setCellStyle(cellStyle);
        headCell = hssfRow.createCell(4);
        headCell.setCellValue("假期状态");
        headCell.setCellStyle(cellStyle);


        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            HolidayMessage holiday = list.get(i);

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(holiday.getId());
            cell.setCellStyle(cellStyle);
            cell = hssfRow.createCell(1);
            cell.setCellValue(holiday.getName());
            cell.setCellStyle(cellStyle);
            cell = hssfRow.createCell(2);
            cell.setCellValue(holiday.getBeginDate());
            cell.setCellStyle(cellStyle);
            cell = hssfRow.createCell(3);
            cell.setCellValue(holiday.getEndDate());
            cell.setCellStyle(cellStyle);
            cell = hssfRow.createCell(4);
            cell.setCellValue(holiday.getReason());
            cell.setCellStyle(cellStyle);

        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream(path);
            holidaybook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }

    @Override
    public List findByDepartment(String department) {
        return holidayDao.findByDepartment(department);
    }

}



